package com.yndsoft.jc.supplier.order;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.OrderAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseFragment;
import com.yndsoft.jc.supplier.model.OrderModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.widget.recyclerview.IRecyclerSwipe;
import com.yndsoft.jc.supplier.widget.recyclerview.refresh.OnLoadMoreListener;
import com.yndsoft.jc.supplier.widget.recyclerview.refresh.RecyclerViewWithFooter;

import java.util.List;

public class OrderFragment extends SupplierBaseFragment implements IRecyclerSwipe, SwipeRefreshLayout.OnRefreshListener, OnLoadMoreListener {

    private SwipeRefreshLayout swipeRefresh;
    private RecyclerViewWithFooter recyclerView;
    private int page = 0;
    private final int COUNT = 10;
    private boolean isRefresh = false;
    private int tag;
    private OrderAdapter orderAdapter;
    private int currentPageIndex = 0;

    @Override
    public int getContentResourceId() {
        return R.layout.supplier_list;
    }

    public static OrderFragment newInstance(int tag) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.TAG, tag);
        OrderFragment orderFragment = new OrderFragment();
        orderFragment.setArguments(bundle);
        return orderFragment;
    }

    @Override
    public void initView() {
        swipeRefresh = findViewById(R.id.swipe_refresh);
        recyclerView = findViewById(R.id.recycler_view);
        swipeRefresh.setColorSchemeColors(0xFFFFBB33, 0xFFFF4444, 0xFF99CC00, 0xFF33B5E5);
        swipeRefresh.setOnRefreshListener(this);
        recyclerView.setOnLoadMoreListener(this);
    }

    @Override
    public void initData() {
        tag = getArguments().getInt(Constants.TAG);
        loadAdapter();
        loadData(0);
    }

    private void loadAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        orderAdapter = new OrderAdapter(mContext);
        recyclerView.setAdapter(orderAdapter);
        orderAdapter.setOnListChangeListener(new OnListChangeListener() {
            @Override
            public void onListChange() {
                mCache.put(Constants.IS_LIST_CHANGE, "true");
                mCache.put(Constants.CHANGE_PAGE_INDEX, String.valueOf(currentPageIndex));
            }
        });
    }

    public void setCurrentPageIndex(int index) {
        currentPageIndex = index;
    }

    public void restartLoadData() {
        String isListChange = mCache.getAsString(Constants.IS_LIST_CHANGE);
        String changePageIndex = mCache.getAsString(Constants.CHANGE_PAGE_INDEX);
        if ("true".equals(isListChange) && currentPageIndex != Integer.parseInt(changePageIndex)) {
            if (orderAdapter != null) {
                orderAdapter.clearData();
            }
            loadData(0);
            mCache.put(Constants.IS_LIST_CHANGE, "false");
        }
    }

    private void loadData(int page) {

        mHttpUtil.supplierOrder(new ProgressSubscriber(mContext, this) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        setEmptyData();
                        break;
                    case 1:
                        paging(returnMsg, true);
                        break;
                    case 2:
                        paging(returnMsg, false);
                        break;
                }
            }
        }, "orderlist", mSession.getSid(), tag, page, COUNT);
    }

    private void paging(ReturnMsg returnMsg, boolean isPaging) {
        String msg = returnMsg.getInfo();
        if (msg != null) {
            List<OrderModel> model = new Gson().fromJson(msg, new TypeToken<List<OrderModel>>() {}.getType());
            if (model != null && model.size() > 0) {
                if (orderAdapter != null) {
                    if (isRefresh) {
                        orderAdapter.setData(model);
                    } else {
                        orderAdapter.addData(model);
                    }
                }
                if (!isPaging) {
                    setNoMore();
                }
            } else {
                setEmptyData();
            }
        }
    }

    private void setEmptyData() {
        if (orderAdapter != null) {
            orderAdapter.clearData();
            orderAdapter.notifyDataSetChanged();
            recyclerView.setEnd("获取数据为空");
        }
    }

    @Override
    public void showItemFail(String msg) {

    }

    @Override
    public void hideSwipeLoading() {
        swipeRefresh.setRefreshing(false);
    }

    @Override
    public void setNoMore() {
        recyclerView.setEnd("没有更多数据");
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 0;
        loadData(page);
    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        page++;
        loadData(page);
    }
}
